package edu.gatech.cs2340.sharks.fms.data;

/**
 * ItemAdapter.java
 * @author Monica Shum
 * 
 * Used to adapt an array of items to a format that can be displayed
 */

import edu.gatech.cs2340.sharks.fms.R;
import android.app.Activity;
import android.content.Context;
import android.view.*;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<Item>
{
	/**
	 * The context from which the adapter is called
	 */
	Context context;
	
	/**
	 * The resource ID of the current layout
	 */
	int layoutResourceId;
	
	/**
	 * The array of items to adapt
	 */
	Item data[] = null; 
	
	public ItemAdapter(Context context, int layoutResourceId, Item[] data)
	{
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View row = convertView;
		ItemHolder holder = null;
        
		if(row == null)
		{
		    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		    row = inflater.inflate(layoutResourceId, parent, false);

		    holder = new ItemHolder();
		    holder.itemIcon = (ImageView)row.findViewById(R.id.itemIcon);
		    holder.itemName = (TextView)row.findViewById(R.id.itemName);
		    holder.details = (Button)row.findViewById(R.id.details); 

		    row.setTag(holder);
		}

		else holder = (ItemHolder)row.getTag();

		Item item = data[position];
		holder.itemName.setText(item.name);
		holder.itemIcon.setImageResource(item.img);

		holder.details.setText("Details"); 
		holder.details.setTag(item.id());  

		return row;
	}
	
	/**
	 * Holds the data associated with an item to allow insertion into the list
	 */
	static class ItemHolder
	{
	    ImageView itemIcon;
	    TextView itemName;
	    Button details;
	}
}
